//
//  SportViewController.swift
//  FengHuangNews
//
//  Created by 新宝 on 2018/12/19.
//  Copyright © 2018年 LaiShengCai. All rights reserved.
//

import UIKit
import Kingfisher

class SportViewController: UIViewController {
    var dataArray:NSArray=NSArray()
    var tableView:UITableView!
    var refreshControl:UIRefreshControl=UIRefreshControl()
    let reuseIdentifier = "Cell"
    
    lazy var titleArray1:[String]={
        ()-> [String] in
        return ["1","2","3","4"]
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
         self.setupUI()
         self.loadData()
    }
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        let key = NSKeyValueChangeKey.newKey
        for dic in change! {
            if(dic.key==key){
                let nameString = dic.value as! String
                if(nameString=="体育新闻"){
                     print("字典12356--------%@",dic.value)
                    self.loadData()
                }
            }
        }
       
    }
    
    private func setupUI() {
        tableView=UITableView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: ScreenHeight), style: .plain)
        tableView.delegate=self as UITableViewDelegate
        tableView.dataSource=self as UITableViewDataSource
        tableView.contentInset=UIEdgeInsets(top: 0, left: 0, bottom: 120, right: 0)
        tableView.rowHeight=100
        tableView.register(SportTableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        //刷新控件
        refreshControl=UIRefreshControl()
        tableView.addSubview(refreshControl)
        self.view.addSubview(tableView)
    }
    
    
   private func loadData(){
    NetworkTool .loadSportsData(controller: "体育") { (sportArr) in
        self.dataArray=sportArr as NSArray
        self.tableView.reloadData()
        print("数据1-------------",self.dataArray)
    }
    
    self.refreshControl.addTarget(self, action: #selector(refreshData), for: UIControl.Event.valueChanged)
    refreshControl.attributedTitle=NSAttributedString(string: "下拉刷新数据")
    
}
    
    
    @objc func refreshData(){
        NetworkTool .loadSportsData(controller: "体育") { (sportArr) in
            self.dataArray=sportArr as NSArray
            self.tableView.reloadData()
            self.refreshControl.endRefreshing()
        }
    }
    
}





extension SportViewController:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = SportTableViewCell(style: .default, reuseIdentifier: reuseIdentifier)
        var sportModel=SportModel()
        sportModel=self.dataArray[indexPath.row] as! SportModel
        let urlString = "http://d.ifengimg.com/w198_h141_q100/p0.ifengimg.com/pmop/2017/0613/E35D45FD500E2A2C116D6B8229094675D634C580_size33_w551_h530.jpeg"
        let url=NSURL(string:(sportModel.thumbnail?.isEmpty)! ? urlString : sportModel.thumbnail!)
        
        // 设置网络图片
        cell.headImageView.kf.setImage(with: ImageResource(downloadURL: url! as URL))
        cell.titleLabel.text=sportModel.title
        cell.timeLabel.text=sportModel.updateTime
        
        cell.selectionStyle = UITableViewCell.SelectionStyle.none;
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var sportModel=SportModel()
        sportModel=dataArray[indexPath.row] as! SportModel
        let webVC=WebViewController()
        webVC.urlString=sportModel.commentsUrl
        self.present(webVC, animated: true, completion: nil)
    }
    
    
}
